﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Services.Client;

namespace Calligraphy.CalligraphyData
{
    static class PhotoLoader
    {
        public const string SmallPhotoSizes60 = "60";
        public const string SmallPhotoSizes110 = "110";
        public const string SmallPhotoSizes250 = "250";
        public const string SmallPhotoSizes400 = "400";
        

        public static  Task<Uri> GetImageUriAsync(T_Photo t_photo)
        {
            return Task.Run(() =>
                {
                    return DataServiceContextHelper.DataContext.GetReadStreamUri(t_photo);
                });
        }

        public static T_Photo GetSmallPhoto(T_Photo photo, string smallPhotoSize)
        {
            var q = from sp in photo.T_Photo1 where sp.Size.Contains(smallPhotoSize.ToString()) && (sp.IsDeleted == null || sp.IsDeleted == false) select sp;
            T_Photo smallPhotoEntity = null;
            try
            {
                smallPhotoEntity = q.First<T_Photo>();
            }
            catch (System.InvalidOperationException e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
            return smallPhotoEntity;

        }

        private static async void LoadSmallPhotoEntity(T_Photo rawPhoto)
        {
            IEnumerable<T_Photo> smallPhotos =
                await
                ((DataServiceQuery<T_Photo>)DataServiceContextHelper.DataContext.T_Photo.Where(p=>(p.IsDeleted == null || p.IsDeleted == false)&&p.RawPhotoID == rawPhoto.ID).Take(20)).ExecuteAsync<T_Photo>();

            foreach (var smallP in smallPhotos)
            {
                rawPhoto.T_Photo1.Add(smallP);
            }
            
        }
    }
}
